package com.ye.common.security.exception;

import com.alibaba.fastjson.JSON;
import com.ye.common.utils.R;
import com.ye.common.utils.StringUtils;
import com.ye.core.constant.HttpStatus;
import com.ye.core.utils.ServletUtils;
import org.springframework.security.core.AuthenticationException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Auther: baby
 * @Date: 2023/10/10 02:12
 * @Description:
 */
public class AuthenticationEntryPoint implements org.springframework.security.web.AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        String msg = StringUtils.format("请求访问：{}，认证失败，无法访问系统资源", request.getRequestURI());
        ServletUtils.renderString(response, JSON.toJSONString(R.fail(HttpStatus.UNAUTHORIZED, msg)));
    }
}
